#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100

//邻接表的结点
typedef struct AJNode //around join
{
    int ajVex;  //邻接点下标
    int weight; //权值
    struct AJNode *pNext;
} AJNODE, *PAJNode;

//邻接表的顶点表
typedef struct VEXNode
{
    int data;
    PAJNode firstAjVex; //指向邻接点的指针
} VEXList[MAXSIZE];

//图
typedef struct Graph
{
    VEXList VList;      //邻接表的顶点表
    int vexNum, arcNum; //图的当前顶点数和边数
} GRAPH, *PGraph;

//查找顶点的下标
int locateVex(GRAPH G, int vex)
{
    for (int i = 0; i < G.vexNum; i++)
    {
        if (vex == G.VList[i].data)
        {
            return i;
        }
        else
        {
            return -1;
        }
    }
}

//创建邻接表的无向网
void createUDG(PGraph G)
{
    //输入顶点数和边数
    printf("输入顶点数和边数");
    scanf("%d %d", &G->vexNum, &G->arcNum);

    //邻接表的顶点表赋值
    for (int i = 0; i < G->vexNum; i++)
    {
        scanf("%c", &G->VList[i].data);
        G->VList[i].firstAjVex = NULL;
    }

    //构造邻接表
    for (int i = 0; i < G->arcNum; i++)
    {
        int v1, v2, weight;
        //输入一条边的两个顶点的值和边的权值
        scanf("%d %d %d", &v1, &v2, &weight);
        int i = locateVex(*G, v1);
        int j = locateVex(*G, v2);
        //创建新结点
        PAJNode p1 = (PAJNode)malloc(sizeof(AJNODE));
        p1->ajVex = j;
        //头插法
        p1->pNext = G->VList[i].firstAjVex;
        G->VList[i].firstAjVex = p1;

        //无向网的对称性
        PAJNode p2 = (PAJNode)malloc(sizeof(AJNODE));
        p2->ajVex = i;
        //头插法
        p2->pNext = G->VList[j].firstAjVex;
        G->VList[j].firstAjVex = p2;
    }
    return 1;
}
